package client.comm.baoding.ui


import android.app.Activity
import android.content.Intent
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import client.comm.commlib.toast.toast
import client.comm.commlib.widget.EmptyLayout
import client.comm.baoding.BaseMiddleActivity
import client.comm.baoding.R
import client.comm.baoding.adapter.AddrMangerAdapter
import client.comm.baoding.databinding.ActivityAddrmangerBinding
import client.comm.baoding.dialog.QrDelAddrDialog
import client.comm.baoding.ui.vm.AddrMangerViewModel

class AddrMangerActivity : BaseMiddleActivity<AddrMangerViewModel, ActivityAddrmangerBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_addrmanger
    }

    val adapter: AddrMangerAdapter by lazy {
        AddrMangerAdapter(this@AddrMangerActivity).apply {
            del = {
                QrDelAddrDialog(this@AddrMangerActivity).apply {
                    qrClick = {
                        viewModel.del(it)
                    }
                }.show()
            }
            edit = {
                startActivity(
                    Intent(
                        this@AddrMangerActivity,
                        AddAddrActivity::class.java
                    ).apply {
                        putExtra("bean", it)
                        putExtra("isEdit", true)
                    })
            }
            itemClick = {
                if (intent.getBooleanExtra("select", false)) {
                    setResult(Activity.RESULT_OK, Intent().apply {
                        putExtra("address", it)
                    })
                    finish()
                }
            }
            defItem = {

                viewModel.defAddr(it)
            }
        }
    }

    override fun initViewModel(): AddrMangerViewModel = createViewModel()

    override fun initView() {
        binding?.apply {
            event = this@AddrMangerActivity
            recyclerView.layoutManager = LinearLayoutManager(this@AddrMangerActivity)
            recyclerView.adapter = adapter
            emptyLayout.setOnClickListener { viewModel.call() }
            refreshLayout.setOnRefreshListener {
                if (!viewModel.isLoading.value!!) {
                    viewModel.call()
                } else {
                    refreshLayout.isRefreshing = false
                }
            }
        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.call()
    }

    override fun observerUI() {
        viewModel.isRef.observe(this, Observer {
            if (!it)
                binding.refreshLayout.isRefreshing = false
        })
        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    data?.let {
                        adapter.submitList(it.address_list)
                        if (it.address_list != null && it.address_list.size > 0) {
                            binding.emptyLayout.visibility = View.GONE
                        } else {
                            binding.emptyLayout.errorType = (EmptyLayout.NODATA)
                        }
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = (EmptyLayout.NETWORK_ERROR)
                }
            }
        })
        viewModel.delRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("删除成功")
                    viewModel.call()
                }
            }
        })
        viewModel.defRet.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("设置成功")
                    viewModel.call()
                }
            }
        })
    }

    fun addAddr() {
        startActivity(Intent(this@AddrMangerActivity, AddAddrActivity::class.java))
    }

}